import { AddTeaItemParam, CommodityListType, TeaItemListType } from "@Tea/model"
import { ReposBase } from "./RepoBase"
import { renderInsertParam, renderInsertSql } from "../utils/renderSql"


export class TeaItemsRepositoty extends ReposBase {
  private static inst: TeaItemsRepositoty

  public static getInstance() {
    if (!TeaItemsRepositoty.inst) {
      TeaItemsRepositoty.inst = new TeaItemsRepositoty()
    }

    return TeaItemsRepositoty.inst
  }

  getTeaItems(teaId: CommodityListType['teaId']) { 
    const sql = `SELECT item_id itemId, item_cover itemCover FROM commodity_items WHERE tea_id = ${teaId}`
    return this.query(sql)
  }

  addTeaItem(params: AddTeaItemParam) { 
    const insert: renderInsertParam = {
      database: 'commodity_items',
      keyAndVal: [
        { key: "item_cover", value: params.itemCover },
        { key: "tea_id", value: params.teaId },
      ]
    }

    return this.insert(renderInsertSql(insert))
  }

  delTeaItem(itemId: TeaItemListType['itemId']) { 
    const sql = `DELETE FROM commodity_items WHERE item_id=${itemId}`

    return this.delete(sql)
  }
}